#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define LENGTH 81										//Maximum input length

int main(int argc, char const *argv[])
{
	char mystr[LENGTH];									//Input string
	int mychar = 0;										//character for output
	FILE *pfile = NULL;									//File pointer
	char *filename = "myfile.txt";

	printf("Enter an interesting string of up to %d characters:\n",LENGTH-1 );
	if(!fgets(mystr,LENGTH,stdin)){						//Read in a string
		printf("Input from keyboard failed.\n");
		exit(1);
	}

	//Create a new file we can write
	if(!(pfile = fopen(filename,"w"))){
		printf("Error opening %s for writing. Program terminated.\n",filename);
		exit(1);
	}

	setvbuf(pfile,NULL,_IOFBF,BUFSIZ);					//Buffer the file
	int len = strnlen(mystr,LENGTH) - 1;
	for(int i=0;i<len;++i)
		fputc(mystr[i],pfile);							//Write string to file backward

	fclose(pfile);										//Close the file


	//Open the file for reading
	if(!(pfile = fopen(filename,"r"))){
		printf("Error opening %s for reading. Program terminated.\n",filename );
		exit(1);
	}
	setvbuf(pfile,NULL,_IOFBF,BUFSIZ);					//Buffer the file


	//Read a character from the file and display it
	printf("the  data read from the file is:\n");		
	while((mychar = fgetc(pfile)) != EOF)
		putchar(mychar);								//Output character from the file

	putchar('\n');										//Write new line

	fclose(pfile);										//Close the file
	pfile = NULL;							
	remove(filename);									//Delete the physical file
	return 0;
}